package com.example.second.service;

import com.example.second.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService{

    @Autowired
    private IUserDao iUserDao;

    public Map<String,Object> getUser(String username ,String password){
        Map<String,Object> resultMap =  new HashMap<String, Object>();

        if(username==null||username==""){
            resultMap.put("result", false);
            resultMap.put("msg", "用户名不能为空");
            return resultMap;
        }

        ArrayList<Map<String,Object>> list =  iUserDao.getUser(username, password);
        if(list.isEmpty()){
            resultMap.put("result", false);
            resultMap.put("msg", "该用户信息不存在");
            return resultMap;
        }

        resultMap = list.get(0);
        if(resultMap.get("password").equals(password)){
            resultMap.put("result", true);
            resultMap.put("msg", "登录成功");
            return resultMap;
        }else {
            resultMap.put("result", false);
            resultMap.put("msg", "密码错误");
            return resultMap;
        }

    }
    public Map<String,Object> getAdminUser(String username ,String password){
        Map<String,Object> resultMap =  new HashMap<String, Object>();

        if(username==null||username==""){
            resultMap.put("result", false);
            resultMap.put("msg", "用户名不能为空");
            return resultMap;
        }

        ArrayList<Map<String,Object>> list =  iUserDao.getAdminUser(username, password);
        if(list.isEmpty()){
            resultMap.put("result", false);
            resultMap.put("msg", "用户信息不存在");
            return resultMap;
        }

        resultMap = list.get(0);
        if(resultMap.get("password").equals(password)){
            resultMap.put("result", true);
            resultMap.put("msg", "登录成功");
            return resultMap;
        }else {
            resultMap.put("result", false);
            resultMap.put("msg", "用户名或密码错误");
            return resultMap;
        }

    }
    public Map<String,Object> insertUser(String username ,String password){
        Map<String,Object> resultMap =  new HashMap<String, Object>();

        if(username==null||username==""){
            resultMap.put("result", false);
            resultMap.put("msg", "用户名不能为空");
            return resultMap;
        }
        int user_id = iUserDao.insertUser(username, password);
        if(user_id<=0){
            resultMap.put("result", false);
            resultMap.put("msg", "注册失败");
            return resultMap;
        }

        resultMap.put("user_id", user_id);
        resultMap.put("username", username);
        resultMap.put("password", password);
        resultMap.put("phone", "");
        resultMap.put("result", true);
        resultMap.put("msg", "登录成功");
        return resultMap;
    }
}
